#include <Arduino.h>
#include <Ticker.h>
#include "Button.h"
#include "Wifi_Ctrl.h"
#include "Menu_Ctrl.h"
#include "WatchDog.h"
#include "Aliyun.h"
#include "WS2812B.h"
#include "Ze08Ch2o.h"
#include "Zph02.h"
#include "Atomizer.h"
#include "DHT11.h"
#include "IIC_Bus.h"
#include "OLED.h"
#include "Fan.h"
#include "Http_Get_Time.h"
#include "Flash.h"

/*静态全局变量*/


/*全局变量*/
extern uint8_t machine_rest_flag;//机器状态 MACHINE_OPEN--开机 MACHINE_CLOSE--关机
Ticker tickersecond;//秒定时器
volatile bool sec_flag = false;//秒标志
volatile uint8_t atomizer_minute_idx = 0;//雾化器满一分钟
volatile uint8_t fan_minute_idx = 0;//风扇满一分钟
volatile uint8_t minute_idx = 0;
//extern volatile uint8_t manual_open_atomizer_flag;//雾化器手动开启标志 0--非自动 1--手动 2--自动
//extern volatile uint8_t manual_open_fan_flag;//风扇自动侦测标志
volatile uint8_t manual_open_atomizer_flag = 1;//雾化器自动侦测标志 
                                               //0--自动侦测关闭 1--自动侦测开启 2--自动侦测暂时关闭 10分钟后继续开启

volatile uint8_t manual_open_fan_flag = 1;//风扇自动侦测标志 
                                          //0-自动侦测关闭 1--自动侦测开启 2--自动侦测暂时关闭10分钟后开启

volatile uint8_t lightstatus = 0;//机器工作状态 0--关闭 1--开启

/*局部函数*/
static void Second_tick(void)
{
  // static uint8_t idx = 0;
  sec_flag = true;
  atomizer_minute_idx++;
  fan_minute_idx++;
  minute_idx++;
}

/*全局函数*/
void setup() 
{
  // put your setup code here, to run once:
  Serial.begin(115200);
  delay(10);

  lightstatus = 1;//机器工作状态设置为开启

  IIC_Init();//IIC初始化

  OLED_Init();//OLED屏幕初始化

  Button_Init();//触摸按键初始化

  WS2812B_Init();//WS2812B灯带初始化

  //Led_Init();//LED灯初始化

  Ze08_Ch2o_Init();//ZE08CH2O甲醛传感器初始化

  Zph02_Init();//ZPH02空气粉尘传感器初始化

  Atomizer_Init();//雾化器初始化

  Fan_Init();//风扇初始化

  while(DHT11_Init())//DHT11初始化
  {
    Serial.println("DHT11 Init Error");
  }

  Serial.println("DHT11 Init success");

  WatchDog_Init();//看门狗初始化

  WiFi_Setup();//WiFi设置初始化

  if(Flash_Init() == 1)//FLASH初始化
  {
    Get_Maunal_Open_Atomizer_Flag_To_Flash(&manual_open_atomizer_flag);
    if(manual_open_atomizer_flag > 1)
    {
      manual_open_atomizer_flag = 1;
    }

    Get_Manual_Open_Fan_Flag_To_Flash(&manual_open_fan_flag);
    if(manual_open_fan_flag > 1)
    {
      manual_open_fan_flag = 1;
    }

    Serial.printf("manual_open_atomizer_flag:%d, manual_open_fan_flag:%d\r\n", manual_open_atomizer_flag, manual_open_fan_flag);
  }

  delay(1000);

  Serial.println("loop start");

  tickersecond.attach(1, Second_tick);//秒定时器开启
  //oled_clear();

  //oled_show_pic(4, 0, 24, 24, menustatus2424[0], 0);//联网 
}

void loop() 
{
  // put your main code here, to run repeatedly:
  uint8_t menu_event = MENU_EVENT_NULL;

  Feed_Dog();//喂狗

  if(sec_flag == true)
  {
    sec_flag = false;
    
    if( (atomizer_minute_idx == 10) && (manual_open_atomizer_flag == 2) )//如果喷雾器处于自动侦测模式暂时关闭 且 1分钟已到 测试
    //if( (atomizer_minute_idx == 60) && (manual_open_atomizer_flag == 2) )//如果喷雾器处于自动侦测模式暂时关闭 且 1分钟已到 准确
    {
      manual_open_atomizer_flag = 1;//喷雾器设置为自动
      Serial.printf("manual_open_atomizer_flag:%d\r\n", manual_open_atomizer_flag);
      Update_Atomizer_Manual_Flag_To_Aliyun(manual_open_atomizer_flag);
      atomizer_minute_idx = 0;
    }

    if( (fan_minute_idx == 10) && (manual_open_fan_flag == 2) )//如果风扇处于自动侦测模式暂时关闭 且 1分钟已到
    //if( (fan_minute_idx == 60) && (manual_open_fan_flag == 2) )//如果风扇处于自动侦测模式暂时关闭 且 1分钟已到 准确
    {
      manual_open_fan_flag = 1;//风扇设置为自动
      Serial.printf("manual_open_fan_flag:%d\r\n", manual_open_fan_flag);
      Update_Fan_Manual_Flag_To_Aliyun(manual_open_fan_flag);
      fan_minute_idx = 0;
    }

    if(minute_idx == 60)
    {
      if(Get_Wifi_State())
      {
        Http_Get_Time();
        Update_LightStatus_To_Aliyun(1);//每60s上传一次机器正常工作状态
      }
      minute_idx = 0;
    }
    
    DHT11_Read_Data();
    menu_event = MENU_EVENT_TICK;
  }

  uint8_t button_state = Get_Button_State();
  if(button_state != 0)
  {
    //Serial.printf("\r\nmain.cpp %d:ESP32 HEAP1 %d\r\n",__LINE__, esp_get_free_heap_size());
    //Serial.printf("button %d\r\n", button_state);
    menu_event = MENU_EVENT_BUTTON1 + button_state - 1;
    //Set_Maunal_Open_Atomizer_Flag_To_Flash(menu_event);
    //Serial.printf("menu_event:%d\r\n",menu_event);
  }

  menu_proc(menu_event);

  if(machine_rest_flag != MACHINE_CLOSE)
  {
    WiFi_Loop();

    Aliyun_Loop();

    Read_Ze08_Ch2o_Data();

    Read_ZPh02_Data();
  }

  Feed_Dog();//喂狗
}